package cn.wolfcode.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.*;
import cn.wolfcode.enums.AppointmentEnums;
import cn.wolfcode.enums.ConsumptionEnum;
import cn.wolfcode.service.*;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {
    @Autowired
    private IConsumptionItemService consumptionItemService;

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequiredPermission(name="消费单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
//        回显枚举
        model.addAttribute("consumptionEnum", ConsumptionEnum.values());
//        回显门店
        model.addAttribute("business",businessService.listAll());
        return "consumption/list";
    }


    @RequiredPermission(name="消费单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="消费单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {

        if (consumption.getId() == null) {

            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="消费单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        Consumption consumption = consumptionService.get(id);
//        回显结算单明细
     List<ConsumptionItem> consumptionItems = consumptionItemService.getByCno(consumption.getCno());
     model.addAttribute("consumptionItems",consumptionItems);
//        回显门店信息
        List<Business> businesses = businessService.listAll();
//        回显业务大类
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.getForTypeId(1L);
        model.addAttribute("systemDictionaryItems",systemDictionaryItems);
//        回显结算类型
        List<SystemDictionaryItem> types = systemDictionaryItemService.getForTypeId(3L);
        model.addAttribute("accountType",types);

        model.addAttribute("businesses",businesses);
        if (id != null) {
            model.addAttribute("consumption",consumptionService.get(id));
        }
        return "/consumption/input";
    }

    @RequestMapping("/save")
    public String save(Long id){
       consumptionService.saveFromAppointment(id);
        return "redirect:/consumption/input?id="+id;
    }
}
